package com.habdqn.easybuy.web.manage.user;

import com.habdqn.easybuy.entity.User;
import com.habdqn.easybuy.service.UserService;
import com.habdqn.easybuy.service.impl.UserServiceImpl;
import com.habdqn.easybuy.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Xing.Jiejian
 * @date 2020/11/28 0028
 */
@WebServlet(name = "UserUpdateServlet", urlPatterns = "/manage/user/update")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strId = request.getParameter("id");
        if (StringUtils.isNotBlank(strId)) {
            int id = Integer.parseInt(strId);
            // 先获取原用户信息
            UserService userService = new UserServiceImpl();
            User user = userService.queryById(id);
            // 更新表单数据
            user.setUsername(request.getParameter("username"));
            user.setPassword(request.getParameter("password"));
            user.setSex(request.getParameter("sex"));
            String strBirthday = request.getParameter("birthday");
            if (StringUtils.isNotBlank(strBirthday)) {
                try {
                    Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse(strBirthday);
                    user.setBirthday(birthday);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            user.setEmail(request.getParameter("email"));
            user.setMobile(request.getParameter("mobile"));
            user.setAddress(request.getParameter("address"));
            // 执行更新业务
            if (userService.update(user) != null) {
                response.setContentType("text/html;charset=utf-8");
                PrintWriter out = response.getWriter();
                out.println("<script>");
                out.println("alert('修改用户成功！');");
                out.println("location.href='" + request.getContextPath() + "/manage/user';");
                out.println("</script>");
            } else {
                request.setAttribute("errorMsg", "修改失败");
                request.setAttribute("user", user);
                request.getRequestDispatcher("/WEB-INF/manage/user/user-modify.jsp").forward(request, response);
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strId = request.getParameter("id");
        if (StringUtils.isNotBlank(strId)) {
            int id = Integer.parseInt(strId);
            // 先获取原用户信息
            UserService userService = new UserServiceImpl();
            User user = userService.queryById(id);
            request.setAttribute("user", user);
            request.getRequestDispatcher("/WEB-INF/manage/user/user-modify.jsp").forward(request, response);
        } else {
            // 未传入id则直接进入列表页
            response.sendRedirect(request.getContextPath() + "/manage/user");
        }
    }
}
